package it.bambo.tankwars.model;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Andreas Baumgartner: andreas@bambo.it
 */
public class BattleField extends JPanel {

    private Rectangle field;
    private List<Unit> units = Collections.synchronizedList(new ArrayList<Unit>());
    ;

    private double sizeFactor = 2;

    public BattleField(Rectangle field) {
        this.field = field;
        setBackground(Color.BLACK);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Unit unit : units) {
            if (unit.getImage() != null) {
                g.drawImage(unit.getImage(), unit.getRect().x, unit.getRect().y, this);
            }
        }
    }

    public List<Unit> getUnits() {
        return units;
    }

    public Rectangle getField() {
        return field;
    }

    public double getSizeFactor() {
        return sizeFactor;
    }

    public Unit intersectsObstacle(Unit unit) {
        return intersectsObstacle(unit, unit.getRect());
    }

    public Unit intersectsObstacle(Unit unit, Rectangle rectangle) {
        for (Unit obstacle : units) {
            if (obstacle != unit && obstacle.getRect().intersects(rectangle)) {
                return obstacle;
            }
        }
        return null;
    }
}
